import 'dart:async';
import 'package:flutter/material.dart';
import 'button.dart';

class MyDialog extends StatelessWidget {
  /// Creates a dialog.
  ///
  /// Typically used in conjunction with [showDialog].
  const MyDialog({
    Key key,
    this.child,
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
    this.radius = 8.0,
  }) : super(key: key);

  /// The widget below this widget in the tree.
  ///
  /// {@macro flutter.widgets.child}
  final Widget child;

  /// The duration of the animation to show when the system keyboard intrudes
  /// into the space that the dialog is placed in.
  ///
  /// Defaults to 100 milliseconds.
  final Duration insetAnimationDuration;

  /// The curve to use for the animation shown when the system keyboard intrudes
  /// into the space that the dialog is placed in.
  ///
  /// Defaults to [Curves.fastOutSlowIn].
  final Curve insetAnimationCurve;

  final double radius;


  Color _getColor(BuildContext context) {
    return Theme.of(context).dialogBackgroundColor;
  }

  @override
  Widget build(BuildContext context) {
    return new AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 60.0, vertical: 24.0),
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
      child: new MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: new Center(
          child: new ConstrainedBox(
            constraints: const BoxConstraints(minWidth: 280.0),
            child: new Material(
              elevation: 24.0,
              color: _getColor(context),
              borderRadius:BorderRadius.all(Radius.circular(radius)),
              type: MaterialType.card,
              child: child,
            ),
          ),
        ),
      ),
    );
  }
}


class MyAlertDialog extends StatelessWidget {

  final String title;
  final Widget content;
  final EdgeInsetsGeometry contentPadding;
  final List<Widget> actions;


  static const TextStyle _titleStyle=TextStyle(
    color:Colors.black,
    fontSize: 16.0
  );

  MyAlertDialog({
    this.title,
    this.content,
    this.contentPadding:const EdgeInsets.symmetric(vertical:8.0,horizontal:18.0),
    this.actions
  });

  @override
  Widget build(BuildContext context) {
    Widget titleWidget;
    if(title!=null){
      titleWidget=Padding(
        padding: EdgeInsets.only(left:8.0,right:8.0,top:18.0),
        child:Text(title,style:_titleStyle),
      );
    }
    else{
      titleWidget=Padding(
        padding: EdgeInsets.only(top:10.0),
      );
    }

    return MyDialog(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children:<Widget>[
            //title
            titleWidget,

            //content
            Padding(
              padding: contentPadding,
              child: content
            ),
            Divider(height: 1.0,color: Colors.grey),

            //actions
            SizedBox(
              height: 35.0,
              child: Row(
                children:actions,
              )
            )
          ]
      )
    );

  }
}

///动作按钮
class MyActionButton extends StatefulWidget {

  final String title;
  final Color  normalColor;
  final Color  pushColor;

  final Color  textColor;
  final Color  textPushColor;
  final double textSize;

  final MyButtonCallback onPress;

  MyActionButton(this.title,{
    this.normalColor:Colors.white,
    this.pushColor:Colors.grey,
    this.textColor,
    this.textPushColor,
    this.textSize,
    this.onPress,
  });

  @override
  _MyActionButtonState createState() => _MyActionButtonState();
}


class _MyActionButtonState extends State<MyActionButton> {

  MyButtonStatus status=MyButtonStatus.Default;

  @override
  Widget build(BuildContext context) {
    Color textColor=widget.textColor;
    Color color=widget.normalColor;
    if(widget.textPushColor!=null && status == MyButtonStatus.Push)
    {
      textColor = widget.textPushColor;
      color = widget.pushColor;
    }

    return Expanded(
      child:GestureDetector(
        child:Material(
          color: color,
          child:Center(
            child:Text(
              widget.title,
              style:TextStyle(
                fontSize:widget.textSize,
                color:textColor
              )
            )
          ) 
        ),
        onTapDown: (dynamic){
          setState(() {
            status = MyButtonStatus.Push;
          });
        },
        onTapCancel: (){
            setState(() {
            status = MyButtonStatus.Default;
          });           
        },
        onTapUp: (dynamic){
          setState(() {
            status = MyButtonStatus.Default;
          });
        },
        onTap: (){
          if(widget.onPress!=null)
            widget.onPress();
        },    
      ),
    );
  }
}


///动作按钮分隔控件
class MyActionDivider extends StatelessWidget {

  final double width;
  final Color  color;

  MyActionDivider({
    this.width:1.0,
    this.color
  });

  @override
  Widget build(BuildContext context) {
    return Material(
      color:color??Theme.of(context).dividerColor,
      child:SizedBox(
        width: width,
        child:Flex(
          direction:Axis.vertical,
        )
      )
    );
  }
}

Future<bool> showMyAlertDialog(BuildContext context,{String title,String content}) async {
  return showDialog<bool>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return new MyAlertDialog(
        title: title,
        content:new Text(content),
        actions: <Widget>[
          new MyActionButton(
            '确定',
            textColor: Colors.black,
            textPushColor: Colors.blue,
            onPress: () {
              Navigator.of(context).pop(true);
            },
          ),
          new MyActionDivider(),
          new MyActionButton(
            '取消',
            textColor: Colors.black,
            textPushColor: Colors.blue,
            onPress: () {
              Navigator.of(context).pop(false);
            },
          ),            
        ],
      );
    },
  );
}
